import { createRouter, createWebHistory, VueRouter, createWebHashHistory } from 'vue-router';
// import store from '@/store';
import { useStore } from 'vuex'; // 假设你使用 Vuex 管理权限

import HomeView from '../views/HomeView.vue';

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView,
    meta: {
      keepAlive: true,
    }
  },
  {
    path: '/about',
    name: 'about',
    component: () =>
      import('../views/shoufeiguize.vue'),
    meta: {
      keepAlive: true,
    },
    children: [
      {
        path: 'computed',
        name: 'computed',
        meta: {
          keepAlive: true,
        },
        component: () => import('../views/computed.vue')
      },
      {
        path: 'twos',
        name: 'twos',
        meta: {
          keepAlive: true,
        },
        component: () => import('../views/twozi.vue')
      },
      {
        path: 'xk',
        name: 'xk',
        meta: {
          keepAlive: true,

        },
        component: () => import('../views/xingkong.vue'),
      },
      {
        path: 'lbt',
        name: 'lbt',
        meta: {
          keepAlive: true,

        },
        component: () => import('../views/lbt.vue'),
      },
      {
        path: 'echarts',
        name: 'echarts',
        meta: {
          keepAlive: true,
        },
        component: () => import('../views/echarts.vue')
      },
      {
        path: 'ditu',
        name: 'ditu',
        meta: {
          keepAlive: true,
          requiresAuth: true,
          roles: ['admin']
        },
        component: () => import('../views/ditu.vue')
      }
    ]
  },
  {
    path: '/down',
    name: 'down',
    meta: {
      keepAlive: true,
    },
    component: () => import('../views/down.vue'),
  },
  {
    path: '/elmplus',
    name: 'elmplus',
    meta: {
      keepAlive: true,

    },
    component: () => import('../views/elmplus.vue'),
  },
  {
    path: '/imgs',
    name: 'imgs',
    meta: {
      keepAlive: true,

    },
    component: () => import('../views/imgs.vue'),
  },
  {
    path: '/lx',
    name: 'lx',
    meta: {
      keepAlive: true,
      isshow: false
    },
    component: () => import('../views/lx.vue'),
  },
  {
    path: '/upload',
    name: 'upload',
    meta: {
      keepAlive: true,

    },
    component: () => import('../views/upload.vue'),
  },
  {
    path: '/websorket',
    name: 'websorket',
    meta: {
      keepAlive: true,

    },
    component: () => import('../views/websorket.vue'),
  },
  {
    path: '/grandf',
    name: 'grandf',
    meta: {
      keepAlive: true,

    },
    component: () => import('../views/grandf.vue'),
  },
  {
    path: '/three',
    name: 'three',
    meta: {
      keepAlive: true,

    },
    component: () => import('../views/three.vue'),
  },
  {
    path: '/dayin',
    name: 'dayin',
    meta: {
      keepAlive: true,

    },
    component: () => import('../views/dayin.vue'),
  },
  {
    path: '/wendang',
    name: 'wendang',
    meta: {
      keepAlive: true,

    },
    component: () => import('../views/wendang.vue'),
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    meta: {
      keepAlive: true,
      isshow: false
    },
    component: () => import('../views/nofund.vue'),
  }
];
// process.env.BASE_URL
const router = createRouter({
  history: createWebHistory(),
  routes
});
;



// 在路由配置中，使用beforeEach钩子来保存当前路由路径
router.beforeEach((to, from, next) => {
  const store = useStore();
  const user = store.state.user;
  const role = store.state.role;



  if (to.meta.requiresAuth) {
    if (!user) {
      console.log('Redirecting to login page');
      next({ name: 'lx' });
    } else if (to.meta.roles && !to.meta.roles.includes(role)) {
      console.log('Redirecting to NotFound page');
      next({ name: 'NotFound' });
    } else {
      next();
    }
  } else {
    next();
  }
});




export default router;
